package com.example.project1.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

import com.example.project1.R;
import com.example.project1.fragment.YongHu_Add_Fragment;
import com.example.project1.fragment.YongHu_Delete_Fragment;
import com.example.project1.fragment.YongHu_Fragment;
import com.example.project1.fragment.YongHu_Updata_Fragment;

public class YongHuActivity extends BaseActivity {
	
	private Fragment[] fragments;
	private int clickedBtnIndex=0;;
	private int currentShowFragmentIndex=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_yonghu);
		
		fragments=new Fragment[]{
				new YongHu_Add_Fragment(),
				new YongHu_Updata_Fragment(),
				new YongHu_Delete_Fragment()
		};
		showFragment();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.yong_hu, menu);
		return true;
	}

	public void showFragment(){
		FragmentTransaction transaction=getSupportFragmentManager().
				beginTransaction();
		
		YongHu_Fragment yonghuFrag=new YongHu_Fragment();
		
		transaction.add(R.id.left_layout,yonghuFrag);
		transaction.add(R.id.right_layout,fragments[0]);
		
		transaction.show(yonghuFrag);
		transaction.show(fragments[0]);
		
		transaction.commit();
		
	}
	
	public void doClick(View v){
		switch(v.getId()){
		case R.id.button_add:
			clickedBtnIndex=0;
			break;
		case R.id.button_updata:
			clickedBtnIndex=1;
			break;
		case R.id.button_delete:
			clickedBtnIndex=2;
			break;
		}
		
		FragmentTransaction transaction=getSupportFragmentManager().
				beginTransaction();
				
				transaction.hide(fragments[currentShowFragmentIndex]);
				
				if(fragments[clickedBtnIndex].isAdded()==false){
					transaction.add(R.id.right_layout, fragments[clickedBtnIndex]);
				}
				
				transaction.show(fragments[clickedBtnIndex]);
				transaction.commit();
				currentShowFragmentIndex=clickedBtnIndex;
	}
	
}
